package com.kehao.accountbook.chart;

import android.graphics.Color;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

public class DrawLineChart {
    LineChart mLineChart;
    XAxis xAxis;
    YAxis leftYAxis;
    int xcount;
    double[] firstLineData;
    double[] seconedLineData;

    public DrawLineChart(LineChart mLineChart) {
        this.mLineChart = mLineChart;
        this.mLineChart.clear();
        //配置linechart
        this.mLineChart.setDrawBorders(false);
        this.mLineChart.setDragEnabled(false);
        this.mLineChart.setDrawGridBackground(false);
        this.mLineChart.setTouchEnabled(false);
        this.mLineChart.setScaleEnabled(false);
        this.mLineChart.setExtraBottomOffset(5);
        xAxis = mLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        leftYAxis = mLineChart.getAxisLeft();
        YAxis rightYAxis = mLineChart.getAxisRight();
        rightYAxis.setEnabled(false); //右侧Y轴不显示
        leftYAxis.setAxisMinimum(0);
        xAxis.setAxisMinimum(1);
        xAxis.setDrawGridLines(false);
        leftYAxis.setDrawGridLines(false);

        Legend legend = mLineChart.getLegend();//图例
        legend.setEnabled(false);//是否显示
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//对齐
        legend.setDrawInside(true);//不是很清楚
        legend.setWordWrapEnabled(false);//设置图列换行(注意使用影响性能,仅适用legend位于图表下面)，我也不知道怎么用的
        legend.setTextColor(Color.BLACK);
    }

    public void draw(int xcount,int ymax,double[] firstLineData,double[] seconedLineData,String titile1,String titile2){
        this.xcount = xcount;
        this.firstLineData =firstLineData;
        this.seconedLineData = seconedLineData;
        xAxis.setLabelCount(xcount, true);
        xAxis.setAxisMaximum(xcount);
        leftYAxis.setAxisMaximum(ymax);
        Description description = mLineChart.getDescription();
        description.setText(""); //设置描述的文字
        //设置数据
        List<Entry> entries1 = new ArrayList<>();
        List<Entry> entries2 = new ArrayList<>();
        for (int i = 1; i <=xcount; i++) {
            entries1.add(new Entry(i, (float) firstLineData[i-1]));
            entries2.add(new Entry(i, (float)seconedLineData[i-1]));
        }
        //一个LineDataSet就是一条线
        //一个LineDataSet就是一条线
        LineDataSet lineDataSet1 = new LineDataSet(entries1, titile1);
        LineDataSet lineDataSet2 = new LineDataSet(entries2, titile2);
        lineDataSet1.setColor(Color.parseColor("#00BFFF"));

        lineDataSet1.setMode(LineDataSet.Mode.LINEAR);
        lineDataSet2.setMode(LineDataSet.Mode.LINEAR);
        lineDataSet2.setColor(Color.RED);

        LineData data1 = new LineData(lineDataSet1);
        mLineChart.setData(data1);
        mLineChart.getLineData().addDataSet(lineDataSet2);

        mLineChart.invalidate();
    }





}
